package com.laiketui.order.utils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.InputStream;
import java.io.OutputStream;

public class ExcelTemplateReader {

    public static Workbook readTemplate(String templateName) throws Exception {
        Resource resource = new ClassPathResource("templates/" + templateName);
        try (InputStream inputStream = resource.getInputStream()) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            return workbook;
        }
    }

    public static void exportWorkbook(Workbook workbook, OutputStream outputStream) throws Exception {
        workbook.write(outputStream);
        workbook.close();
    }

    public static void main(String[] args) {
        try {
            String templateName = "template.xlsx";
            Workbook workbook = readTemplate(templateName);

            // 示例：输出到控制台
            try (OutputStream outputStream = System.out) {
                exportWorkbook(workbook, outputStream);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
